﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace KeyCtrl
{
    public class Shortcut
    {
        private String _VSName, _shortDesc, _longDesc;

        public Shortcut(String VSName, String shortDesc, String longDesc)
        { 
            _VSName = String.Copy(VSName); 
            _shortDesc = String.Copy(shortDesc); 
            _longDesc = String.Copy(longDesc); 
        }

        public String GetVSName() {return String.Copy(_VSName);}
        public String GetShortDesc() {return String.Copy(_shortDesc);}
        public String GetLongDesc() { return String.Copy(_longDesc); }
    }

    public static class ShortcutReader
    {
        public static LinkedList<String> GetLanguages (XmlNode database)
        {
            
            XmlNode node = database;
            LinkedList<String> ret = new LinkedList<String>();

            if (!node.HasChildNodes)
                return ret;

            node = node.FirstChild;
            ret.AddLast(node.Attributes.Item(0).Value);

            while (!IsLastSibling(node))
            {
                node = node.NextSibling;
                ret.AddLast(node.Attributes.Item(0).Value);
            }

            return ret;
        }

        public static LinkedList<String> GetTabs (XmlNode database, String language)
        {
            XmlNode node = database;
            LinkedList<String> ret = new LinkedList<String>();

            //if any languages are set
            if (!node.HasChildNodes)
                return null;

            // sets node to the first languge
            node = node.FirstChild;

            while (!node.Attributes.Item(0).Value.Equals(language) && IsLastSibling(node))
                node = node.NextSibling;

            if (!node.Attributes.Item(0).Value.Equals(language))
                return null;
            else if(!node.HasChildNodes)
                return ret;

            // sets node to the first tab
            node = node.FirstChild;
            ret.AddLast(node.Attributes.Item(0).Value);

            while (!IsLastSibling(node))
            {
                node = node.NextSibling;
                ret.AddLast(node.Attributes.Item(0).Value);
            }

            return ret;
        }

        //[TODO] remove this line at the end of the project.  XmlDocument is implementation of interface XmlNode 
        public static LinkedList<Shortcut> GetShortcuts(XmlNode database, String language, String tab)
        {
            XmlNode node = database;

            //if any languages are set
            if (!node.HasChildNodes)
                return null;

            // sets node to the first languge
            node = node.FirstChild;

            while (!node.Attributes.Item(0).Value.Equals(language) && !IsLastSibling(node))
                node = node.NextSibling;

            if (!node.Attributes.Item(0).Value.Equals(language) || !node.HasChildNodes)
                return null;

            //sets node to the first tab;
            node = node.FirstChild;

            while (!node.FirstChild.Value.Equals(tab) && !IsLastSibling(node))
                node = node.NextSibling;

            if (!node.FirstChild.Value.Equals(tab))
                return null;

            //sets node to the name of the tab
            node = node.FirstChild;

            LinkedList<Shortcut> ret = new LinkedList<Shortcut>();

            while (!IsLastSibling(node))
            {
                node = node.NextSibling;
                ret.AddLast(
                    new Shortcut(
                        node.FirstChild.Value, 
                        node.FirstChild.NextSibling.Value,
                        node.FirstChild.NextSibling.NextSibling.Value));
            }

            return ret;
        }

        private static bool IsLastSibling(XmlNode node) { return (node == node.ParentNode.LastChild); }
    }
}
